package com.hhp.cloud.shop.admin.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhp.cloud.shop.admin.service.PermissionService;
import com.hhp.cloud.shop.admin.pojo.vo.user.LoginUserVO;
import com.hhp.cloud.shop.common.web.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import com.hhp.cloud.shop.admin.pojo.entity.UserEntity;
import com.hhp.cloud.shop.admin.service.UserService;

import com.hhp.cloud.shop.common.core.result.R;



/**
 * 用户信息表
 *
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-11-23 10:25:32
 */
@Api(tags = "用户管理")//加apikinf4j会显示
@RestController
@RequestMapping("/api/v1/admin/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final PermissionService permissionService;
    private final PasswordEncoder passwordEncoder;

    /**
     * 列表
     */
    @ApiOperation(value = "用户信息分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String")
    })
    @GetMapping
    public R list(Integer page, Integer limit, String username){
        return R.ok().put("data", userService.page(
                new Page<>(page, limit),
                new LambdaQueryWrapper<UserEntity>()
                        .like(StrUtil.isNotBlank(username), UserEntity::getUsername, username)));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "用户信息详情")
    @ApiImplicitParam(name = "id", value = "用户信息id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Integer id){
        return R.ok().setData(userService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增用户信息")
    @PostMapping
    public R save(@RequestBody UserEntity user){
        //passwordEncoder.encode(user.getPassword())
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改用户信息")
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Long id, @RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除用户信息")
    @DeleteMapping
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation(value = "获取当前登陆的用户信息")
    @GetMapping("/me")
    public R info(){
        LoginUserVO loginUserVO = new LoginUserVO();
        // 用户基本信息
        Long userId = UserUtils.getUserId();
        UserEntity userEntity = userService.getById(userId);
        BeanUtil.copyProperties(userEntity, loginUserVO);
        // 用户角色信息
        List<String> roles = UserUtils.getRoles();
        loginUserVO.setRoles(roles);
        // 用户按钮权限信息
        List<String> perms = permissionService.listBtnPermByRoles(roles);
        loginUserVO.setPerms(perms);
        return R.ok().setData(loginUserVO);
    }

    @ApiOperation(value = "根据用户名获取认证信息",notes = "提供用于用户登录认证信息")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "path", dataType = "String")
    @GetMapping("/username/{username}")
    public R getUserByUsername(@PathVariable String username) {
        return R.ok().setData(userService.getByUsername(username));
    }

}
